<?php
/*==========================================================================*\
| AGRIVIET - CONTENT MANAGERMENT SYSTEM						
\*==========================================================================*/


	//$pathdb = "uploads/download"; // Duong dan ghi vao database		
	$imagepath="../uploads/download/"; // Duong dan de upload Image
	$upload_allow_extension = "jpg,png,gif"; // Cac phan mo rong duoc cho phep

if (isset($_POST['butSave'])) {
	$published = $_POST['published'];
	$title = $_POST['title'];
	$alias = vietdecode($title);
	$intro = $_POST['intro'];
	$fileurl = $_POST['fileurl'];
	$date_add = mktime();
	$catid = $_POST['catid'];
	//$cat = getC($catid);
	//$catlink = 'download/'.$cat->alias;
	//$cattitle = $cat->title;
	$hit = $_POST['hit'];
	
	$err="";	
	if ($title=="") $err .="Xin nhập tiêu đề<br>";	
	if ($intro =="") $err .="Xin nhập intro bằng<br>";	
	
	
	if ($err==''){
		/* Cap nhat hoac them moi */
		if (!empty($_POST['id'])) {
			$oldid = $_POST['id'];
			$sql = "update download set  title='".$title."',alias='".$alias."', intro='".$intro."',fileurl='".$fileurl."',hit='".$hit."',";
			$sql.= "published='".$published."', ";
			$sql.= "catid='".$catid."' where id='".$oldid."'";
		}else {
			// Insert data
			$sql = "insert into download (title,alias,cattitle,fileurl,intro,published,catid,hit,date_add)";
			$sql.= "values ('".$title."','".$alias."','".$fileurl."'";
			$sql.= ",'".$intro."','".$published."','".$catid."','".$hit."','".$date_add."')";
		}
		/* EOF - Cap nhat hoac them moi */
		if(mysql_query($sql)){ // Neu thuc hien query thanh cong
		
			if(empty($_POST['id'])) $oldid = mysql_insert_id();
			
			/*if(!empty($_POST['id'])){
			// upldate
				foreach ($_POST['title'] as $langid => $title){		
						$intro = trim($_POST['intro'][$langid]);
						$body = trim($_POST['body'][$langid]);		
						$sql = "update products_des set title='".$title."',intro='".$intro."'";
						$sql .= "where id='".$oldid."' and  languages_id='".$langid."'";
						mysql_query($sql);
					}	
			} else {
			// Insert			
				foreach ($_POST['title'] as $langid => $title){						
					$sql = "insert into products_des (id,languages_id,title,intro)";
					$sql .= "values('".$oldid."','".$langid."','".$title."','".$intro."')";
					mysql_query($sql);
				}	
			}*/
			
		//Upload image here
		$filename="";
		if(is_uploaded_file($_FILES['img']['tmp_name']))
		{
			//Check file extension
			if (check_upload_extension($_FILES['img']['name'],$upload_allow_extension) == 1){
			$filename = generate_name('download',$oldid,$_FILES['img']['name']);
			move_uploaded_file($_FILES['img']['tmp_name'],$imagepath .'large/'. $filename);
			resize_image($imagepath.'large/'.$filename,$imagepath.'small/'.$filename,120,200,100);
			resize_image($imagepath.$filename,$imagepath.'large/'.$filename,500,800,100);

			$_POST["img"] = $filename;
					
			$sqlUpdateField = " image ='".$filename."'";
			
				if($sqlUpdateField!='')
				{
					$sqlUpdate = "update download set $sqlUpdateField where id='".$oldid."'";
					mysql_query($sqlUpdate);
				}
			}
		} else {
			$_POST["img"] = "";
		}
		// Eof Upload image
		} // Neu thuc hien Query thanh cong
		else {
			$err =  "Không th&#7875; c&#7853;p nh&#7853;t";
		}
		
	} // if ($err=='')
	
	if ($err==''){
	//$strSQL = "SELECT * from download order by date_add DESC limit 7";
	//echo homelist($strSQL,'../modules/html/download.htm');
	echo '<script>window.location="index.php?act=download&code=1"</script>';
	}else { 
	echo "<p align=center class='red'>".$err."</p>";
	}

} 
			
 action('Product');
if (isset($_GET['id'])){
	$result=mysql_query("SELECT * FROM download WHERE id=$id");
	$row=mysql_fetch_object($result);
	$title=$row->title;
	$image=$row->image;
	//$largeimage=$row->largeimage;
	$intro=$row->intro;
	$published=$row->published;
	$fileurl=$row->fileurl;
	$hit=$row->hit;
	$catid=$row->catid;
	$mid=$row->mid;
	$parent=$catid;
	}
?>
  <table border="0" cellpadding="3" cellspacing="3" bordercolor="#CCCCCC">
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="hidden" name="act" value="download_m">
<input type="hidden" name="id" value="<? echo $_REQUEST['id']; ?>">
<input type="hidden" name="page" value="<? echo $_REQUEST['page']; ?>">

	<tr>
      <td width="20%" align="right" valign="middle">Download name: </td>
      <td width="76%">  <input name="title" type="text" value="<?=$title;?>" size="50"></td>
    </tr>

	<tr>
      <td align="right" valign="top">Description:</td>
      <td valign="top" ><textarea name="intro" rows="4" id="intro"><?=$intro;?></textarea></td>
	</tr>
	 
	<tr>
	  <td align="right">Category: </td>
	  <td ><select size="1" name="catid">
        <?
		echo "<option value='0'>Root</option>";
		$cats=getListCat('','download_categories');
		foreach ($cats as $cat)
		{
			if ($cat[0]==$catid)
				echo "<option value=".$cat[0]." selected>".$cat[1]."</option>";
			else
				echo "<option value=".$cat[0].">".$cat[1]."</option>";
		}
		?>
      </select></td>
	</tr>
	<tr>
      <td align="right" valign="bottom">Image:</td>
      <td ><? if($image!='') {?>
	  <a href="../<?=$image;?>" target="_blank">
	  <img src="../uploads/download/small/<?=$image;?>" border="0" /></a><br /><? } ?>
	  <input name="img" type="file" id="img"></td>
	</tr>
	<tr>
	  <td align="right">File Url: </td>
	  <td ><input name="fileurl" type="text" id="fileurl" value="<?=$fileurl;?>" size="50" /></td>
	</tr>
	<tr>
	  <td align="right">Hit: </td>
	  <td ><input name="hit" type="text" id="hit" value="<?=$hit;?>" /></td>
	</tr>
	<tr>
	  <td align="right">Published: </td>	  
	  <td ><input name="published" type="checkbox" id="published" value="1" <? if ($published>0) echo 'checked' ?> /></td>
	</tr>
	<? 
	$sql=mysql_query("SELECT * FROM languages order by ordering ASC");
	while($r=mysql_fetch_object($sql)){
	?>
    <? } ?>
    <tr>
      <td>&nbsp;</td>
      <td><label>
        <input name="butSave" type="submit" id="butSave" value="Update" class="button">
        <input name="date_add" type="hidden" id="date_add" value="<?=mktime();?>" />
      </label></td>
    </tr></form>
</table>

